#include <gtk/gtk.h>
#include "widgets.h"
#include "password_generator.h"

gint generate_single(GtkWidget * widget, gpointer data)
{
	const gchar   * keyword       = gtk_entry_get_text(GTK_ENTRY(                    keyword_entry_1));
	const gchar   * service       = gtk_entry_get_text(GTK_ENTRY(                    service_entry));
	const gchar   * username      = gtk_entry_get_text(GTK_ENTRY(                    username_entry)); 
	gint            length        = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(length_entry));
	gint            special       = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(  special_entry));

	// Check entries for input
	gboolean missing_field = FALSE;
	if (keyword[0] == 0)
	{
		gtk_entry_set_placeholder_text(GTK_ENTRY(keyword_entry_1), "Keyword required");
		missing_field = TRUE;
	}
	if (service[0] == 0)
	{
		gtk_entry_set_placeholder_text(GTK_ENTRY(service_entry), "Service required");
		missing_field = TRUE;
	}
	if (username[0] == 0)
	{
		gtk_entry_set_placeholder_text(GTK_ENTRY(username_entry), "Username required");
		missing_field = TRUE;
	}
	if (missing_field)
	{
		return -1;
	}
	gtk_entry_set_placeholder_text(GTK_ENTRY(keyword_entry_1), "Keyword");
	gtk_entry_set_placeholder_text(GTK_ENTRY(service_entry), "Service");
	gtk_entry_set_placeholder_text(GTK_ENTRY(username_entry), "Username");

	// generate password and write to output
	gchar password[length + 1];
	generate_password(password, keyword, service, username, length, special);
	// Create output
	gchar * output_text = g_strdup_printf("/--------------------------------%s\n%s\n%s\n\n", 
				service, 
				username, 
				password);
	// Clear and write to output
	gtk_text_buffer_set_text(output_buffer, output_text, -1);
	g_free(output_text);

	return 0;
}
